﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace EstevesRecordersProject
{
    /// <summary>
    /// Interaction logic for WindowEditRecorder.xaml
    /// </summary>
    /// 
    
    public partial class WindowEditRecorder : Window
    {
        private String message;
        public String Message { get { return message; } set { message = value; lblMessage.Content = value; } }

        public Recorder recorder = null;

        private void InitializeComboBoxRecorders()
        {
            List<Recorder> recorders = Repository.GetAllRecorders();
            comboBoxRecorders.ItemsSource = recorders;
            comboBoxRecorders.SelectedValuePath = "ID";
        }

        public WindowEditRecorder()
        {
            try
            {
                InitializeComponent();
                InitializeComboBoxRecorders();
                Message = "";
            }
            catch (Exception ex)
            {
                 Message = ex.Message;
                 lblMessage.Foreground = Brushes.Red;
            }
        }

        private void MakeLineDataCanvas()
        {
            try
            {
                Message = "";
                canvasLine.Children.Clear();
                //zxczczx
                String tbText = tbNrOfLines.Text;
                int nrLines = Int32.Parse(tbText);

                for (int i = 0; i < nrLines; i++)
                {
                    Label l = new Label();
                    l.Name = "lblLine" + (i + 1).ToString();
                    l.Content = "Line " + (i + 1).ToString() + " :";
                    l.Margin = new Thickness(lblRecorderName.Margin.Left, 10 + i * 30, 0, 0);

                    TextBox tb = new TextBox();
                    tb.Text = "line_" + (i + 1).ToString();
                    tb.Height = 23;
                    tb.Width = 160;
                    tb.Name = "tb" + (i + 1).ToString();
                    tb.Margin = new Thickness(tbRecorderName.Margin.Left, 10 + i * 30, 0, 0);

                    canvasLine.Children.Add(l);
                    canvasLine.Children.Add(tb);
                }

                this.canvasLine.Height = (nrLines) * 30;

            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void tbNrOfLines_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Message = "";

                String tbText = tbNrOfLines.Text;
                int nrLines = Int32.Parse(tbText);

                MakeLineDataCanvas();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (recorder == null) throw new NoRecorderSelectedException();

                recorder.name = tbRecorderName.Text;
                recorder.name2 = tbTechnicalName.Text;

                Repository.EditRecorder(recorder);

                int nr = Int32.Parse(tbNrOfLines.Text);
                int nr2 = recorder.NrOfLines;

                int i = 0;
                foreach (Control c in canvasLine.Children)
                {
                    if (c is TextBox)
                    {
                        i++;
                        TextBox tb = c as TextBox;
                        String _name = tb.Text;

                        if (i <= nr2)
                        {
                            Line line = recorder.Line(i);
                            line.name = _name;
                            Repository.EditLine(line);
                        }
                        else
                        {
                            Repository.AddNewLine(recorder.id, _name, i);
                        }
                    }
                }

                if (nr < nr2)
                {
                    for (int j = nr + 1; j <= nr2; j++ )
                    {
                        Repository.DeleteLine(recorder.id, j);
                    }
                }

                Message = "Recorder " + recorder + " successfully edited.";
                lblMessage.Foreground = Brushes.Green;

                Process.Start(Application.ResourceAssembly.Location);
                Application.Current.Shutdown();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void comboBoxRecorders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                String s = comboBoxRecorders.SelectedValue.ToString();
                int idRecorder = Int32.Parse(s);
                recorder = Repository.GetRecorderById(idRecorder);

                InitializeWindow();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void InitializeWindow()
        {
            try
            {
                if (recorder != null)
                {

                    tbRecorderName.Text = recorder.name;
                    tbTechnicalName.Text = recorder.name2;
                    tbNrOfLines.Text = recorder.NrOfLines.ToString();

                    int nr = 0;
                    foreach (Control c in canvasLine.Children)
                    {
                        if (c is TextBox)
                        {
                            nr++;
                            TextBox tb = c as TextBox;
                            tb.Text = recorder.Line(nr).name;
                        }
                    }
                }
                else
                {
                    tbRecorderName.Text = "";
                    tbTechnicalName.Text = "";
                    tbNrOfLines.Text = "0";
                }

            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (recorder == null) throw new NoRecorderSelectedException();

                MessageBoxResult mbr =  MessageBox.Show("Are you sure that you want to delete this recorder?", "Sure?", MessageBoxButton.YesNo);

                if (mbr == MessageBoxResult.No)
                {
                    return;
                }

                Repository.DeleteRecorder( recorder.id );

                recorder = null;
                InitializeComboBoxRecorders();
                
                InitializeWindow();

                Message = "Recorder " + recorder + " deleted successfully. ";
                lblMessage.Foreground = Brushes.Green;

                Process.Start(Application.ResourceAssembly.Location);
                Application.Current.Shutdown();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }
    }
}
